

package practicabeta;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioSystem;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class NuevoPiloto extends javax.swing.JFrame {
javax.sound.sampled.Clip sonido;
boolean verificacion = false;
  File file;
  ButtonGroup bc;
  int eleccion;
    public NuevoPiloto() {
        initComponents();
        bc=new ButtonGroup();
        bc.add(camion);
        bc.add(cabezal);
    }

   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cnit = new javax.swing.JTextField();
        cfoto = new javax.swing.JTextField();
        cd = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        clic = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        prefoto = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        camion = new javax.swing.JRadioButton();
        cabezal = new javax.swing.JRadioButton();
        jButton4 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        cced = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        ctel = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        cdir = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        cfam = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        cesp = new javax.swing.JTextField();
        cnom = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        cnit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cnitActionPerformed(evt);
            }
        });

        cd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cdActionPerformed(evt);
            }
        });

        jLabel7.setText("F o t o ");

        jLabel1.setText("C o d i g o ");

        clic.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clicActionPerformed(evt);
            }
        });

        jLabel3.setText("Licencia");

        jLabel2.setText("N o m b r e");

        jLabel5.setText("T i p o    P i l o t o ");

        jLabel4.setText("Edad");

        prefoto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes Proy Supervisada/Pilotos.gif"))); // NOI18N

        jButton3.setText("Cancerlar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton1.setText("Ok  ");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        camion.setSelected(true);
        camion.setText("Camion ");

        cabezal.setText("Cabezal ");

        jButton4.setText("C a r g a r ");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel6.setText("Cedula ");

        cced.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ccedActionPerformed(evt);
            }
        });

        jLabel8.setText("Telefono ");

        ctel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ctelActionPerformed(evt);
            }
        });

        jLabel9.setText("Dirección");

        cdir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cdirActionPerformed(evt);
            }
        });

        jLabel11.setText("Integrantes en Familia ");

        cfam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cfamActionPerformed(evt);
            }
        });

        jLabel12.setText("Nombre Esposa ");

        cesp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cespActionPerformed(evt);
            }
        });

        cnom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cnomActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(27, 27, 27)
                        .addComponent(cd, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(172, 172, 172)
                                .addComponent(jButton1)
                                .addGap(18, 18, 18)
                                .addComponent(jButton3))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel7)
                                        .addGap(61, 61, 61)
                                        .addComponent(cfoto, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(90, 90, 90)
                                        .addComponent(jButton4)
                                        .addGap(18, 18, 18)
                                        .addComponent(prefoto, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel5)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(cnit, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(camion)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 87, Short.MAX_VALUE)
                                                .addComponent(cabezal))))))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel11)
                                    .addComponent(jLabel12)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel9)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cnom, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                                    .addComponent(clic, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                                    .addComponent(cced, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                                    .addComponent(ctel, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                                    .addComponent(cdir, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                                    .addComponent(cesp, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                                    .addComponent(cfam, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE))))
                        .addGap(30, 30, 30)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(cnom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(clic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(cced, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(ctel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addComponent(cdir, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(cfam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(cesp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(cnit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(camion)
                    .addComponent(cabezal))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cfoto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(prefoto))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(jButton4)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cnitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cnitActionPerformed
      
}//GEN-LAST:event_cnitActionPerformed

    private void cdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cdActionPerformed
       
}//GEN-LAST:event_cdActionPerformed

    private void clicActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clicActionPerformed
      
}//GEN-LAST:event_clicActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        dispose();
        Piloto va;

        va = new Piloto();
        va.setVisible(true);
        Dimension pantalla, cuadro;
        pantalla = Toolkit.getDefaultToolkit().getScreenSize();
        cuadro = va.getSize();
        va.setLocation(((pantalla.width - cuadro.width)/2),(pantalla.height - cuadro.height)/2);
        va.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            Insertar();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(NuevoPiloto.class.getName()).log(Level.SEVERE, null, ex);
        }
if (verificacion==true){
   dispose();
        Piloto va;

        va = new Piloto();
        va.setVisible(true);
        // centramos la pantalla
        Dimension pantalla, cuadro;
        pantalla = Toolkit.getDefaultToolkit().getScreenSize();
        cuadro = va.getSize();
        va.setLocation(((pantalla.width - cuadro.width)/2),(pantalla.height - cuadro.height)/2);
        va.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed


        // abro el archivo tipo "jpg"
        FileDialog filAbrir = new FileDialog(this,"Abrir archivo", FileDialog.LOAD);
        filAbrir.show( );

        cfoto.setText(""+filAbrir.getDirectory() +  filAbrir.getFile());


        // generare un preview

        preview();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void ccedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ccedActionPerformed
       
    }//GEN-LAST:event_ccedActionPerformed

    private void ctelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ctelActionPerformed
     
    }//GEN-LAST:event_ctelActionPerformed

    private void cdirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cdirActionPerformed
      
    }//GEN-LAST:event_cdirActionPerformed

    private void cfamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cfamActionPerformed
       
    }//GEN-LAST:event_cfamActionPerformed

    private void cespActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cespActionPerformed
       
    }//GEN-LAST:event_cespActionPerformed

    private void cnomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cnomActionPerformed
       
    }//GEN-LAST:event_cnomActionPerformed
// metodo preview sirve para que el usuario se asegure qee es la imagen la qee selecciono . ;D
    public void preview (){

     ImageIcon im = new ImageIcon(cfoto.getText());
     // creare una condicion cuando la imagen sea demasiado grande

     if(im.getIconWidth()>105 || im.getIconHeight()> 118){
         ImageIcon imaescala = new ImageIcon(im.getImage().getScaledInstance(105, 118, 100));
         prefoto.setIcon(imaescala);
     }
     else {
          prefoto.setIcon(im);
              }



}
  public void Insertar() throws FileNotFoundException{
     try {
            int resultado = 0 ;
// codio para conectarme 
            MysqlDataSource dataSource = new MysqlDataSource();
            dataSource.setUser("root");
            dataSource.setPassword("");
            dataSource.setDatabaseName("practicasp8");
            dataSource.setServerName("localhost");
             // coneccion
            Connection conexion = (Connection) dataSource.getConnection();

            PreparedStatement ps = null;
            ps = (PreparedStatement) conexion.prepareStatement("insert into piloto (idpiloto,nombre,licencia,cedula,telefono,direccion,nomesposa,numintegrantes," +
                    "edad,tipopiloto,foto) values (?,?,?,?,?,?,?,?,?,?,?)");

            
             try{
             ps.setInt(1,Integer.parseInt( cd.getText()));


             }catch(Exception e ){
                 JOptionPane.showMessageDialog(rootPane, "Dato no ingresado error en el dato de Numero economico de Piloto. ");

             }
            
            ps.setString(2,cnom.getText()); // con el ps genero lo qee voy a ingresar
            //lo genero en orden el 1 va a ser el dato uno en este caso seria nombre
            // y como nombre es varchar tonces sera string


            // licencia
            ps.setString(3, clic.getText());

// cedula
           ps.setString(4, cced.getText());

// telefono
           ps.setString(5, ctel.getText());
//direccion
            ps.setString(6, cdir.getText());
//nombre de la esposa
             ps.setString(7, cesp.getText());
//numero de integrantes en la familia
            ps.setString(8, cfam.getText());

 //edad
           try{
             ps.setInt(9,Integer.parseInt( cnit.getText()));


             }catch(Exception e ){
                 JOptionPane.showMessageDialog(rootPane, "Dato no ingresado error en el dato edad"+resultado);

             }

              try{
                  if (camion.isSelected()){
                          eleccion=1;

                  }
                  else
                      eleccion=2;

             ps.setInt(10,(eleccion));


             }catch(Exception e ){
                 JOptionPane.showMessageDialog(rootPane, "Dato no ingresado  Revisar Manual"+resultado);

             }
// Dato Cuatro
              //Foto
       if (cfoto.getText()!=null){
             file = new File(cfoto.getText()); // el file nuevo
             // sera el que acabavos de abrir
       }
       else {
       file=new File(System.getProperty ("user.dir")+"\\src\\Imagenes Proy Supervisada\\Pilotos.gif");
       }
             // prepara para meter los datos
             FileInputStream fis = null;

            try {
                fis = new FileInputStream(file);
            } catch (FileNotFoundException ex) {

               
            }

             ps.setBinaryStream(11, fis, (int) file.length());

               
             resultado = ps.executeUpdate();
             // sonido


try
{
  sonido = null;
  sonido=AudioSystem.getClip();
  sonido.open(AudioSystem.getAudioInputStream(new File(System.getProperty ("user.dir")+"\\src\\SonidosPractica\\Correcto2.wav")));
  sonido.start();




}catch(Exception e)
  {System.out.println(""+e);} // fin

            JOptionPane.showMessageDialog(rootPane, "Dato ingresado exitosamente"+resultado);
            verificacion=true;
            
               conexion.close(); // cierro la conexion

             }catch(SQLException e){

          JOptionPane.showMessageDialog(rootPane, "Número economico del piloto repetido debera cambiarlo");
          verificacion=false;
        
           } // fin del catch
}
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NuevoPiloto().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton cabezal;
    private javax.swing.JRadioButton camion;
    private javax.swing.JTextField cced;
    private javax.swing.JTextField cd;
    private javax.swing.JTextField cdir;
    private javax.swing.JTextField cesp;
    private javax.swing.JTextField cfam;
    private javax.swing.JTextField cfoto;
    private javax.swing.JTextField clic;
    private javax.swing.JTextField cnit;
    private javax.swing.JTextField cnom;
    private javax.swing.JTextField ctel;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel prefoto;
    // End of variables declaration//GEN-END:variables

}
